package com.ruoyi.system.V2.financial.service.impl;

import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.V2.financial.mapper.FinanceMapper;
import com.ruoyi.system.V2.financial.service.IFinanceService;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.model.domain.Finance;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 财务报表Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-24
 */
@Service
public class FinanceServiceImpl implements IFinanceService {
    @Autowired
    private FinanceMapper financeMapper;

    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    /**
     * 查询财务报表
     *
     * @param id 财务报表主键
     * @return 财务报表
     */
    @Override
    public Finance selectFinanceById(Long id) {
        return financeMapper.selectFinanceById(id);
    }

    /**
     * 查询财务报表列表
     *
     * @param finance 财务报表
     * @return 财务报表
     */
    @Override
    public List<Finance> selectFinanceList(Finance finance) {

        /**
         * 排序
         */
        if (!StringUtils.isNull(finance.getSort())) {
            String sort = finance.getSort().replace("ending", "");
            finance.setSort(sort);
        }
        if (!StringUtils.isNull(finance.getSortColumn())) {
            finance.setSortColumn(humpToLine(finance.getSortColumn()));
        }

        boolean flag = true;
        try {
            Long userId = SecurityUtils.getUserId();
            if (userId != 1l) {
                finance.setCreateId(userId);
            }
        } catch (ServiceException serviceException) {
            flag = false;
        }
        if (!flag) {
            return new ArrayList<>();
        }
        return financeMapper.selectFinanceList(finance);
    }

    private String humpToLine(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString().toUpperCase();
    }

    /**
     * 新增财务报表
     *
     * @param finance 财务报表
     * @return 结果
     */
    @Override
    public int insertFinance(Finance finance) {
        Long userId = SecurityUtils.getUserId();
        finance.setCreateId(userId);
        finance.setCreateTime(DateUtils.getNowDate());
        return financeMapper.insertFinance(finance);
    }

    @Override
    public int insertFinanceH5(Finance finance) {
        finance.setCreateTime(DateUtils.getNowDate());
        return financeMapper.insertFinance(finance);
    }

    /**
     * 修改财务报表
     *
     * @param finance 财务报表
     * @return 结果
     */
    @Override
    public int updateFinance(Finance finance) {
        Long userId = SecurityUtils.getUserId();
        finance.setUpdateId(userId);
        finance.setUpdateTime(DateUtils.getNowDate());
        return financeMapper.updateFinance(finance);
    }

    /**
     * 批量删除财务报表
     *
     * @param ids 需要删除的财务报表主键
     * @return 结果
     */
    @Override
    public int deleteFinanceByIds(Long[] ids) {
        return financeMapper.deleteFinanceByIds(ids);
    }

    /**
     * 删除财务报表信息
     *
     * @param id 财务报表主键
     * @return 结果
     */
    @Override
    public int deleteFinanceById(Long id) {
        return financeMapper.deleteFinanceById(id);
    }

    @Override
    public String importAccount(List<Finance> financeList) {
        Long userId = SecurityUtils.getUserId();
        for (Finance finance : financeList) {
            finance.setCreateId(userId);
            finance.setCreateTime(new Date());
            financeMapper.insertFinance(finance);
        }
        return null;
    }

    @Override
    public Map getShzd(Long id) {
        Map<String, Finance> returnMap = new HashMap<>();
        Finance finance = selectFinanceById(id);
        if (Objects.isNull(finance)) {
            throw new GlobalException("未找到此数据");
        }
        Finance financeReq = new Finance();
        financeReq.setField4(finance.getField4());
        List<Finance> finances = selectFinanceList(financeReq);
        if (CollectionUtils.isNotEmpty(finances)) {
            for (int i = 0; i < finances.size(); i++) {
                Finance finance1 = finances.get(i);
                if (finance1.getBillType() == 9) {
                    returnMap.put("xszd", finance1);
                } else if (finance1.getBillType() == 13) {
                    returnMap.put("hszd", finance1);
                } else if (finance1.getBillType() == 16) {
                    returnMap.put("shzd", finance1);
                }
            }
        }
        return returnMap;
    }
}
